package com.lc.service.system;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.lc.dao.system.UserMapper;
import com.lc.entity.system.User;
import com.lc.service.BaseServiceImpl;


@Service
public class UserService extends BaseServiceImpl<UserMapper, User>{

	public User getUserByEmail(String email) {
		User user = new User();
		user.setEmail(email);
		User dbUser = mapper.selectByNameOrEmail(user);
		if (dbUser != null) {
			return dbUser;
		}
		return null;
	}

	public int insertOrUpdate(User user) {
		int result = 0;
		if (user != null && user.getId() > 0) {
			User dbuser = mapper.selectOne(user.getId());
			if (dbuser != null) {
				dbuser.setUsername(user.getUsername());
				dbuser.setEmail(user.getEmail());
				dbuser.setPhone(user.getPhone());
				result = mapper.update(dbuser);
			} else {
				result = mapper.insert(user);
			}
		}
		return result;
	}

	public void delete(String ids) {
		if (StringUtils.isNotEmpty(ids)) {
			String[] id = ids.split(",");
			for (String i : id) {
				if (StringUtils.isNumeric(i)) {
					mapper.delete(Integer.parseInt(i));
				}
			}
		}
	}
}
